package com.derive.education_service.web.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.entity.Business;
import com.derive.education_service.mapper.BusinessMapper;
import com.derive.education_service.utils.MyFileUtils;
import com.derive.education_service.web.platform.bean.dto.BusinessAdd;
import com.derive.education_service.web.platform.bean.dto.BusinessUp;
import com.derive.education_service.web.platform.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 主营业务实现接口
 */
@Service
@Component
@Slf4j
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private MyFileUtils myFileUtils;

    /**
     * 新增机构主营业务数据
     *
     * @return
     */
    @Override
    public ResponseMessage addBusiness(MultipartFile file, BusinessAdd businessAdd) {
        // 数据填充
        Business business = new Business();
        BeanUtils.copyProperties(businessAdd, business);
        // 文件上传
        try {
            ResponseMessage r = myFileUtils.uploadFile(file, "business");
            if (r.getStatus() == 0) {
                // 成功
                business.setBusinessIcon(String.valueOf(r.getData()));
            } else {
                // 失败
                log.error("主营业务文件上传异常{[新增]}：" + r.getMsg());
                return r;
            }
        } catch (IOException e) {
            log.error("机构主营业务文件上传异常{[新增]}");
            e.printStackTrace();
            return ResponseMessage.createByErrorMessage("机构主营业务文件上传异常{[新增]}");
        }
        // 生成唯一标识
        String uuid = UUID.randomUUID().toString().replace("-", "");
        business.setCode(uuid);
        business.setCreateTime(new Date());
        business.setUpdateTime(new Date());
        // 插入数据
        int row = businessMapper.insert(business);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("数据异常");
        } else {
            return ResponseMessage.createBySuccessMessage("成功");
        }
    }

    /**
     * 删除机构主营业务数据 -- 根据id -- 不能为空
     *
     * @param businessId
     * @return
     */
    @Override
    public boolean delBusiness(Integer businessId) {
        log.info("删除机构主营业务数据{[]}");
        // 删除资源
        String icon = businessMapper.selectById(businessId).getBusinessIcon();
        // 删除原来的文件
        ResponseMessage dr = myFileUtils.delFile(icon);
        if (dr.getStatus() == 1) {
            // 失败退出
            log.error("删除异常{[delBusiness]}");
            return false;
        }
        int row = businessMapper.deleteById(businessId);
        return row <= 0 ? false : true;
    }

    /**
     * 删除机构下所有的主营业务数据 -- 机构id
     *
     * @param platformId
     * @return
     */
    @Override
    public boolean batchDelBusiness(Integer platformId) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("platform_id", platformId);
        // 删除资源
        List<Business> icons = businessMapper.selectList(queryWrapper);
        for (Business b : icons) {
            // 删除原来的文件
            ResponseMessage dr = myFileUtils.delFile(b.getBusinessIcon());
            if (dr.getStatus() == 1) {
                // 失败退出
                log.error("删除异常{[batchDelBusiness]}");
                return false;
            }
        }
        int row = businessMapper.delete(queryWrapper);
        log.info("删除机构下" + row + "条主营业务{[]}");
        return row <= 0 ? false : true;
    }

    /**
     * 编辑机构主营业务数据
     * @return
     */
    @Override
    public ResponseMessage upBusiness(MultipartFile file, BusinessUp businessUp) {
        Business business = new Business();
        BeanUtils.copyProperties(businessUp, business);
        // 判断是否更新文件
        if (file != null && !file.isEmpty()) {
            // 更新了文件 -- 先删除原来的文件
            String icon = businessMapper.selectById(businessUp.getBusinessId()).getBusinessIcon();
            // 删除原来的文件
            ResponseMessage dr = myFileUtils.delFile(icon);
            if (dr.getStatus() == 1) {
                // 失败退出
                return dr;
            }
            // 文件上传
            try {
                ResponseMessage r = myFileUtils.uploadFile(file, "business");
                if (r.getStatus() == 0) {
                    // 成功 -- 设置新的icon地址
                    business.setBusinessIcon(String.valueOf(r.getData()));
                } else {
                    // 失败
                    log.error("主营业务文件上传异常{[编辑]}：" + r.getMsg());
                    return r;
                }
            } catch (IOException e) {
                log.error("机构主营业务文件上传异常{[编辑]}");
                e.printStackTrace();
                return ResponseMessage.createByErrorMessage("机构主营业务文件上传异常");
            }
        }
        business.setUpdateTime(new Date());
        // 更新数据
        int row = businessMapper.updateById(business);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("异常");
        } else {
            return ResponseMessage.createBySuccessMessage("成功");
        }
    }

    /**
     * 获取指定机构下的主营业务(菜单) -- 不分页 -- 基本信息
     *
     * @param platformId
     * @return
     */
    @Override
    public List<Business> getBusinessListByPlatformId(Integer platformId) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("platform_id", platformId);
        List<Business> businessList = businessMapper.selectList(queryWrapper);
        return businessList;
    }

    /**
     * 根据id查询指定业务信息 -- 详细信息
     *
     * @param businessId
     * @return
     */
    @Override
    public Business getBusinessById(Integer businessId) {
        Business business = businessMapper.selectById(businessId);
        return business;
    }
}
